package cn.edu.ujn.liuklab3.service;

import cn.edu.ujn.liuklab3.entity.Dish;
import cn.edu.ujn.liuklab3.dao.DishDao;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

@Service("dishService")
public class DishServiceImpl implements DishService {
    @Resource
    private DishDao dishDao;

    @Override
    public Dish queryById( Integer dishId ) {
        return this.dishDao.queryById(dishId);
    }

    @Override
    public Page<Dish> queryByPage(Dish dish, PageRequest pageRequest) {
        long total = this.dishDao.count(dish);
        return new PageImpl<>(this.dishDao.queryAllByLimit(dish, pageRequest), pageRequest, total);
    }

    @Override
    public Dish insert(Dish dish) {
        this.dishDao.insert(dish);
        return dish;
    }

    @Override
    public Dish update(Dish dish) {
        this.dishDao.update(dish);
        return null;
    }

    @Override
    public boolean deleteById( Integer dishId ) {
        return this.dishDao.deleteById(dishId) > 0;
    }

    @Override
    public cn.edu.ujn.util.Page<Dish> selectAll(Dish dish) {
        List<Dish> dishes = dishDao.selectAll(dish);
        cn.edu.ujn.util.Page<Dish> page = new cn.edu.ujn.util.Page<>();
        page.setPage(dish.getStart());
        page.setRows(dishes);
        page.setSize(dish.getRows());
        page.setTotal(dishDao.selectCount());
        return page;
    }

    @Override
    public List<Dish> findAll() {
        return dishDao.findAll();
    }
}
